<?php

if (! $wpdb) {
	@require ('../../../wp-config.php');
}
if (! $wpdb) {
	echo 'WP Database not available. Quitting';
	die ();
}
include ('pclzip.lib.php');
$oc_upload_dir = ABSPATH . PLUGINDIR . "/oneclick/upload/";
$oc_temp_dir = ABSPATH . PLUGINDIR . "/oneclick/temp/";
$oc_theme_dir = ABSPATH . 'wp-content/themes/';
$oc_plugin_dir = ABSPATH . PLUGINDIR;
$oc_confirmation_required = false;
$oc_filename = ".zip";
$oc_confirmation_array = array ( );
function oc_debug($string, $var = 0) {
	echo "<br>Debug Output:$string.<br>VarDump:";
	if ($var) {
		var_dump ( $var );
	}
	//vardump($var);
}

function oc_can_execute() //TODO: Should return true on *nix based systems. Allows easy unzipping
{
	return false;
}
function oc_execute($command) {
	exec ( $command, $output );
	oc_debug ( "Executed \"$command\" with output \"$output\"" );
}
function oc_clear_directory($path, $match) {
	oc_debug ( "Deleting folder: ", $path );
	if (oc_can_execute ()) {
		$command = "rm " . $path . "*.*"; //Clear out the temporary directory 
		oc_execute ( $command );
	} else {
		$match = "*";
		static $deld = 0, $dsize = 0;
		$dirs = glob ( $path . "*" );
		$files = glob ( $path . $match );
		foreach ( $files as $file ) {
			if (is_file ( $file )) {
				$dsize += filesize ( $file );
				unlink ( $file );
				$deld ++;
			}
		}
		foreach ( $dirs as $dir ) {
			if (is_dir ( $dir )) {
				$dir = basename ( $dir ) . "/";
				oc_clear_directory ( $path . $dir, $match );
			}
		}
		return "$deld files deleted with a total size of $dsize bytes";
	}
}
function oc_process_confirmation() {
	//First, let's check for plugins.
	global $oc_confirmation_required, $oc_temp_dir;
	if ($oc_confirmation_required) {
		$oc_plugins = oc_get_plugins ();
		if ($oc_plugins ['Name'] != 'oc_error_notfound') {
			$oc_confirmation_required = false;
			oc_clear_directory ( $oc_temp_dir, "*" );
			oc_debug("OC PLUGINS HAS VALUE PATH AS:",$oc_plugins);
			return $oc_plugins;
		}
		oc_clear_directory ( $oc_temp_dir, "*" );
		return - 1;
	}
	return 0;
	
//TODO: Check for themes
}
function oc_process_file($file_path, $file_name) {
	
	global $oc_temp_dir, $oc_upload_dir, $oc_filename;
	$oc_filename = $file_name;
	if (oc_can_execute ()) {
		$command = "rm " . $oc_temp_dir . "*.*"; //Clear out the temporary directory 
		oc_execute ( $command );
		$command = "unzip $file_path -d $oc_temp_dir";
		oc_execute ( $command );
	} else {
		oc_clear_directory ( $oc_temp_dir, "*" );
		$archive = new PclZip ( $file_path );
		oc_debug ( "Attempting to unzip $file_path" );
		if ($archive->extract ( PCLZIP_OPT_PATH, $oc_temp_dir ) == 0) {
			die ( "Error : " . $archive->errorInfo ( true ) );
		}
	}
	global $oc_confirmation_required;
	$oc_confirmation_required = true;
}
function oc_upload($file_name, $file_type, $plugin_path, $file_activate = 0) {
	global $oc_temp_dir, $oc_upload_dir, $oc_filename, $oc_plugin_dir, $oc_theme_dir;
	oc_debug("oc_upload called with parameters: $file_name, $file_type, $plugin_path, $file_activate");
	$file_path = $oc_upload_dir . $file_name;
	if ($file_type == 'plugin') {
		if (oc_can_execute ()) {
			$command = "unzip $file_path -d $oc_plugin_dir";
			oc_execute ( $command );
			$command = "rm " . $oc_upload_dir . "*.*"; //Clear out the temporary directory 
			oc_execute ( $command );
		
		} else {
			$archive = new PclZip ( $file_path );
			if ($archive->extract ( PCLZIP_OPT_PATH, $oc_plugin_dir ) == 0) {
				die ( "Error : " . $archive->errorInfo ( true ) );
			}
			oc_clear_directory ( $oc_upload_dir, "*" );
		}
		if ($file_activate) //activate the plugin
		{
			$current = get_option ( 'active_plugins' );
			$plugin = $plugin_path;
			oc_debug("Activation of $plugin step 1. Current=",$current);
			if (! in_array ( $plugin, $current )) {
				//wp_redirect ( 'plugins.php?error=true' ); // we'll override this later if the plugin can be included without fatal error
				ob_start ();
				@include (ABSPATH . PLUGINDIR . '/' . $plugin);
				$current [] = $plugin;
				sort ( $current );
				update_option ( 'active_plugins', $current );
				do_action ( 'activate_' . $plugin );
				ob_end_clean ();
				oc_debug("Activation of $plugin step 2. Current=",$current);
			}
			
		}
	}
	if ($file_type == 'theme') {
		if (oc_can_execute ()) {
			$command = "unzip $file_path -d $oc_theme_dir";
			oc_execute ( $command );
			$command = "rm " . $oc_upload_dir . "*.*"; //Clear out the temporary directory 
			oc_execute ( $command );
		
		} else {
			
			$archive = new PclZip ( $file_path );
			if ($archive->extract ( PCLZIP_OPT_PATH, $oc_theme_dir ) == 0) {
				die ( "Error : " . $archive->errorInfo ( true ) );
			}
			oc_clear_directory ( $oc_upload_dir, "*" );
		}
		if($file_activate)
		{
			if ('activate' == $_GET['action']) {
			update_option('template', $plugin_path);
			update_option('stylesheet', $plugin_path);
		do_action('switch_theme', get_current_theme());
		}
		}
	}
}
?>
